package com.yc.web.fillter;

import com.yc.model.JsonModel;
import com.yc.utils.YcConstants;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author zwei
 * @create 2024-06-16 11:11
 * 容器技术(生命周期  容器技术=》tomcat vue)
 */
@WebFilter(value = {"/cust.action","/resorder.action","/food.action"})
public class RightFilter extends BaseFilter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //doFilter类似于Servlet中的service方法
        //配置的请求都会经过这个方法
        //判断httpsession中是否有 resuser这个键  如果没有则说明未登录
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        HttpSession session=req.getSession();
        String op=req.getParameter("op");
        if (session.getAttribute(YcConstants.RESUSER)!=null){
            //表明是已经登录的用户  直接向后
            filterChain.doFilter(req,resp);  //只有执行了这句话在继续往下走
        }else if ("findFoodsPage".equals(op)||"findAllFoods".equals(op)){
            filterChain.doFilter(req,resp);  //只有执行了这句话在继续往下走
        }else {
            //是要过滤的地址  未登录
            JsonModel jm=new JsonModel();
            jm.setCode(0);
            jm.setError("用户未登录");
            super.writeJson(jm,resp);
        }
    }

    @Override
    public void destroy() {
    }
}
